using UnityEngine;

[RequireComponent(typeof(SkeletonAnimation), typeof(Rigidbody2D))]
public class SpineboyController : MonoBehaviour
{
	private SkeletonAnimation skeletonAnimation;

	public string idleAnimation = "idle";

	public string walkAnimation = "walk";

	public string runAnimation = "run";

	public string hitAnimation = "hit";

	public string deathAnimation = "death";

	public float walkVelocity = 1f;

	public float runVelocity = 3f;

	public int hp = 10;

	private string currentAnimation = string.Empty;

	private bool hit;

	private bool dead;

	private void Start()
	{
		skeletonAnimation = GetComponent<SkeletonAnimation>();
	}

	private void Update()
	{
		if (dead)
		{
			return;
		}
		float axis = UnityEngine.Input.GetAxis("Horizontal");
		float num = Mathf.Abs(axis);
		if (!hit)
		{
			if (axis > 0f)
			{
				skeletonAnimation.skeleton.FlipX = false;
			}
			else if (axis < 0f)
			{
				skeletonAnimation.skeleton.FlipX = true;
			}
			if (num > 0.7f)
			{
				SetAnimation(runAnimation, loop: true);
				Rigidbody2D component = GetComponent<Rigidbody2D>();
				float x = runVelocity * Mathf.Sign(axis);
				Vector2 velocity = GetComponent<Rigidbody2D>().velocity;
				component.velocity = new Vector2(x, velocity.y);
			}
			else if (num > 0f)
			{
				SetAnimation(walkAnimation, loop: true);
				Rigidbody2D component2 = GetComponent<Rigidbody2D>();
				float x2 = walkVelocity * Mathf.Sign(axis);
				Vector2 velocity2 = GetComponent<Rigidbody2D>().velocity;
				component2.velocity = new Vector2(x2, velocity2.y);
			}
			else
			{
				SetAnimation(idleAnimation, loop: true);
				Rigidbody2D component3 = GetComponent<Rigidbody2D>();
				Vector2 velocity3 = GetComponent<Rigidbody2D>().velocity;
				component3.velocity = new Vector2(0f, velocity3.y);
			}
		}
		else if (skeletonAnimation.state.GetCurrent(0).Animation.Name != hitAnimation)
		{
			hit = false;
		}
	}

	private void SetAnimation(string anim, bool loop)
	{
		if (currentAnimation != anim)
		{
			skeletonAnimation.state.SetAnimation(0, anim, loop);
			currentAnimation = anim;
		}
	}

	private void OnMouseUp()
	{
		if (hp > 0)
		{
			hp--;
			if (hp == 0)
			{
				SetAnimation(deathAnimation, loop: false);
				dead = true;
				return;
			}
			skeletonAnimation.state.SetAnimation(0, hitAnimation, loop: false);
			skeletonAnimation.state.AddAnimation(0, currentAnimation, loop: true, 0f);
			Rigidbody2D component = GetComponent<Rigidbody2D>();
			Vector2 velocity = GetComponent<Rigidbody2D>().velocity;
			component.velocity = new Vector2(0f, velocity.y);
			hit = true;
		}
	}
}
